﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using intelliPaper.DB;
using intelliPaper.Model;
using intelliSys.Management.Automation.Test.Properties;
using System.Xml.Serialization;
using System.Linq;

namespace intelliSys.Management.Automation.Test
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            this.InitializeComponent();
            this.MouseLeftButtonDown += new System.Windows.Input.MouseButtonEventHandler(MainWindow_MouseLeftButtonDown);
            // Insert code required on object creation below this point.
        }


        private void MainWindow_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown(0);
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            if (G_TP.IsChecked.Value)
            {
                TestPaper testPaper = new TestPaper();
                Paper pp = new Paper();
                pp.TestDate = DateTime.Now;
                testPaper.CommandType = WindowCommandType.CreateNew;
                Settings.Default.TP = pp;
                testPaper.DataContext = pp;
                this.Visibility = System.Windows.Visibility.Hidden;
                testPaper.ShowDialog();
                this.Visibility = System.Windows.Visibility.Visible;
                if (testPaper.DialogueResult == WindowCommandResult.CreateNewSavingChanges)
                {
                    pp.ID = Dao.AddPaper(pp);
                    DataRepository.Papers.Add(pp);
                }
            }
            if (C_TP.IsChecked.Value)
            {
                GenericList list = new GenericList();
                list.commandBox.SelectedIndex = 1;
                list.commandBox.IsEnabled = false;
                list.ShowDialog();
                if (list.DialogueResult == null)
                {
                    return;
                }
                var bkup = (list.DialogueResult as Paper).CreateDeepCopy();
                TestPaper tp = new TestPaper();
                Settings.Default.TP = (Paper)(tp.DataContext = list.DialogueResult);
               Helper.FillQuestions(DataRepository.Questions, (tp.DataContext as Paper).Questions);
                this.Visibility = System.Windows.Visibility.Hidden;
                tp.CommandType = WindowCommandType.Open;
                tp.ShowDialog();
                this.Visibility = System.Windows.Visibility.Visible;
                if (tp.DialogueResult == WindowCommandResult.OpenSavingChanges)
                {
                    if ((tp.DataContext as Paper).CompareTo(bkup) == 0)
                    {
                        return;
                    }
                    (tp.DataContext as Paper).ID = Dao.AddPaper(tp.DataContext as Paper);
                    DataRepository.Papers.Add(bkup);
                }
                else
                {
                    (list.DialogueResult as Paper).Restore(bkup);
                }
            }
            if (A_KP.IsChecked.Value)
            {
                var WndBehaviorResult = WindowBehaviorCommand.CreateMore;
                while (WndBehaviorResult == WindowBehaviorCommand.CreateMore)
                {
                    KnowledgePoint knowledgePoint = new KnowledgePoint();
                    TestCriteriaMgmt tm = new TestCriteriaMgmt();
                    tm.CommandType = WindowCommandType.CreateNew;
                    tm.DataContext = knowledgePoint;
                    Settings.Default.KP = knowledgePoint;
                    this.Visibility = System.Windows.Visibility.Hidden;
                    tm.ShowDialog();
                    WndBehaviorResult = tm.WindowBehavior;
                    this.Visibility = System.Windows.Visibility.Visible;
                    if (tm.DialogueResult == WindowCommandResult.CreateNewSavingChanges)
                    {
                        knowledgePoint.ID = Dao.AddKnowledgePoint(knowledgePoint);
                        DataRepository.KnowledgePoints.Add(knowledgePoint);
                    }
                }
            }
            if (C_KP.IsChecked.Value)
            {
                GenericList list = new GenericList();
                list.commandBox.SelectedIndex = 2;
                list.commandBox.IsEnabled = false;
                list.ShowDialog();
                if (list.DialogueResult == null)
                {
                    return;
                }
                TestCriteriaMgmt tp = new TestCriteriaMgmt();
                Settings.Default.KP = (KnowledgePoint)(tp.DataContext = list.DialogueResult);
                this.Visibility = System.Windows.Visibility.Hidden;
                var bkup = (tp.DataContext as KnowledgePoint).CreateDeepCopy();
                tp.ShowDialog();
                this.Visibility = System.Windows.Visibility.Visible;
                if (tp.DialogueResult == WindowCommandResult.OpenSavingChanges)
                {
                    if ((tp.DataContext as KnowledgePoint).CompareTo(bkup) == 0)
                    {
                        return;
                    }
                    (list.DialogueResult as KnowledgePoint).ID = Dao.AddKnowledgePoint(list.DialogueResult as KnowledgePoint);
                    DataRepository.KnowledgePoints.Add(bkup);
                }
                else
                {
                    (list.DialogueResult as KnowledgePoint).Restore(bkup);
                }
            }
            if (A_Q.IsChecked.Value)
            {
                var WndBehviorResult = WindowBehaviorCommand.CreateMore;
                while (WndBehviorResult == WindowBehaviorCommand.CreateMore)
                {
                    QuestionMgmt qm = new QuestionMgmt();
                    qm.CommandType = WindowCommandType.CreateNew;
                    Question q = new Question();
                    Settings.Default.Q = q;
                    qm.DataContext = q;
                    this.Visibility = System.Windows.Visibility.Hidden;
                    qm.ShowDialog();
                    WndBehviorResult = qm.WindowBehavior;
                    this.Visibility = System.Windows.Visibility.Visible;
                    if (qm.DialogueResult == WindowCommandResult.CreateNewSavingChanges)
                    {
                        q.ID = Dao.AddQuestion(q);
                        foreach (HistoryMessage hm in q.HistoryMessages)
                        {
                            Dao.AddQuestionHistroyMessage(q, hm);
                        }
                        DataRepository.Questions.Add(q);
                    }
                }
            }
            if (C_Q.IsChecked.Value)
            {
                GenericList list = new GenericList();
                list.commandBox.SelectedIndex = 0;
                list.commandBox.IsEnabled = false;
                list.ShowDialog();
                if (list.DialogueResult == null)
                {
                    return;
                }
                var bkup = (list.DialogueResult as Question).CreateDeepCopy();
                QuestionMgmt tp = new QuestionMgmt();
                Settings.Default.Q = (Question)(tp.DataContext = list.DialogueResult);
                this.Visibility = System.Windows.Visibility.Hidden;
                tp.ShowDialog();
                this.Visibility = System.Windows.Visibility.Visible;
                if (tp.DialogueResult == WindowCommandResult.OpenSavingChanges)
                {
                    if ((tp.DataContext as Question).CompareTo(bkup) != 0)
                    {
                        (list.DialogueResult as Question).ID = Dao.AddQuestion(list.DialogueResult as Question);
                        DataRepository.Questions.Add(bkup);
                    }
                    foreach (HistoryMessage hm in (list.DialogueResult as Question).HistoryMessages)
                    {
                        if (!bkup.HistoryMessages.Contains(hm))
                        {
                            Dao.AddQuestionHistroyMessage((list.DialogueResult as Question), hm);
                        }
                    }
                }
                else
                {
                    (list.DialogueResult as Question).Restore(bkup);
                }
            }
            if (V_S.IsChecked.Value)
            {
                throw new NotImplementedException();
            }
            if (E_D_CT.IsChecked.Value)
            {
                GenericList list = new GenericList();
                list.commandBox.SelectedIndex = 0;
                list.Comfirm.Visibility = System.Windows.Visibility.Collapsed;
                list.Cancel.Content = "完成";
                list.ShowDialog();
            }
        }

    }
}
